package com.sxbbc.agent.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sxbbc.agent.common.MappingUtils;
import com.sxbbc.agent.service.AgentDataService;
import com.sxbbc.agent.util.AgentSession;
import com.sxbbc.common.basics.rest.RestFactory;
import com.sxbbc.common.basics.rest.RestResponse;
import com.sxbbc.common.basics.utils.statics.StaticUtils;
import com.sxbbc.common.core.entity.jz.*;
import com.sxbbc.common.core.enums.HandleTypeEnums;
import com.sxbbc.common.core.service.ICustomerLevelInfoService;
import com.sxbbc.common.core.service.IIndustryService;
import com.sxbbc.common.core.utils.BasePage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 代理用户查看
 *
 * @author leehao
 * @date 2019-07-10
 */
@Controller
public class DataController {


    @Autowired
    private AgentDataService agentDataService;
    @Autowired
    private IIndustryService iIndustryService;
    @Autowired
    private ICustomerLevelInfoService customerLevelInfoService;

    /**
     * 收益页面
     * @param details
     * @author leehao
     * @return org.springframework.web.servlet.ModelAndView
     */
    @GetMapping(value = MappingUtils.SHOW_PROFIT_LIST)
    public ModelAndView showProfitList(AgDonateDetail details){
        ModelAndView view = new ModelAndView(MappingUtils.SHOW_PROFIT_LIST);
        List<HandleTypeEnums> handleList = HandleTypeEnums.queryList();
        view.addObject("handleList", handleList);
        view.addObject("details", details);
        return view;
    }

    /**
     * 数据接口 查询收益明细接口
     * @param request
     * @param page
     * @author leehao
     * @return com.sxbbc.common.basics.rest.RestResponse
     */
    @RequestMapping(value = MappingUtils.QUERY_PROFIT_LIST)
    @ResponseBody
    public RestResponse queryProfitList(HttpServletRequest request, BasePage<AgDonateDetail> page, AgDonateDetail agDonateDetail){
        AgAgent agent = AgentSession.getAgent(request);
        if (agent == null) {
            return RestFactory.fail("获取代理信息失败");
        }
        agentDataService.queryProfitList(page,agent,agDonateDetail);
        return RestFactory.success(page.getRecords(),page.getTotal());
    }

    /**
     * 页面 代理区域用户列表
     * @author leehao
     * @return org.springframework.web.servlet.ModelAndView
     */
    @GetMapping(value = MappingUtils.SHOW_CUSTOMER_LIST)
    public ModelAndView showCustomerList(CCustomer customer){
        ModelAndView view = new ModelAndView(MappingUtils.SHOW_CUSTOMER_LIST);
        List<CustomerLevelInfo> levelList = customerLevelInfoService.list(new QueryWrapper<>());
        view.addObject("levelList", levelList);
        view.addObject("customer",customer);
        return view;
    }

    /**
     * 数据接口 代理区域用户列表
     * @param request 获取代理对象
     * @param page 分页
     * @author leehao
     * @return com.sxbbc.common.basics.rest.RestResponse
     */
    @RequestMapping(value = MappingUtils.QUERY_CUSTOMER_LIST)
    @ResponseBody
    public RestResponse queryCustomerList(HttpServletRequest request, BasePage<CCustomer> page,CCustomer customer){
        AgAgent agent = AgentSession.getAgent(request);
        if (agent == null) {
            return RestFactory.fail("获取代理信息失败");
        }
        agentDataService.queryCustomerList(page,agent,customer);
        return RestFactory.success(page.getRecords(),page.getTotal());
    }

    /**
     * 页面 代理区域商家列表
     * @author leehao
     * @return org.springframework.web.servlet.ModelAndView
     */
    @GetMapping(value = MappingUtils.SHOW_BUSINESS_LIST)
    public ModelAndView showBusinessList(BBusiness business){
        ModelAndView view = new ModelAndView(MappingUtils.SHOW_BUSINESS_LIST);
        QueryWrapper<Industry> wrapper = new QueryWrapper<>();
        wrapper.eq("status", StaticUtils.STATUS_YES);
        List<Industry> industryList = iIndustryService.list(wrapper);
        view.addObject("industryList",industryList);
        view.addObject("business",business);
        return view;
    }

    /**
     * 数据接口 代理区域商家列表
     * @param request 获取代理对象
     * @param page 分页对象
     * @author leehao
     * @return com.sxbbc.common.basics.rest.RestResponse
     */
    @RequestMapping(value = MappingUtils.QUERY_BUSINESS_LIST)
    @ResponseBody
    public RestResponse queryBusinessList(HttpServletRequest request, BasePage<BBusiness> page, BBusiness business){
        AgAgent agent = AgentSession.getAgent(request);
        if (agent == null) {
            return RestFactory.fail("获取代理信息失败");
        }
        agentDataService.queryBusinessList(page,agent,business);
        return RestFactory.success(page.getRecords(),page.getTotal());
    }

}
